package zmee.pur.src.opplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.scm.common.util.SrmCommonUtil;
import kd.scm.pds.common.enums.SrcNegotiatetypeEnums;
import kd.scm.src.common.bidopen.SrcBidOpenFacade;
import kd.scm.src.common.negopen.SrcNegOpenFacade;
import org.apache.commons.lang.StringUtils;

import java.util.Iterator;
import java.util.List;

/**
 * @Author: xw
 * @Date: 2023/01/03/10:12
 * @Description:
 */
public class SrcNegotiateAuditOpExt extends AbstractOperationServicePlugIn {
    public SrcNegotiateAuditOpExt() {
    }

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        e.getFieldKeys().add("template");
        e.getFieldKeys().add("tplentry.tmp_bizobject");
        e.getFieldKeys().add("tplentry.tmp_template");
        e.getFieldKeys().add("tplentry.srctplid");
        e.getFieldKeys().add("negotiatetype");
        e.getFieldKeys().add("opentype");
        e.getFieldKeys().add("billstatus");
        e.getFieldKeys().add("bizstatus");
        e.getFieldKeys().add("project");
        e.getFieldKeys().add("turns");
    }

    @Override
    public void beforeExecuteOperationTransaction(BeforeOperationArgs evt) {
        super.beforeExecuteOperationTransaction(evt);
        List<ExtendedDataEntity> dataEntitys = evt.getValidExtDataEntities();
        String opKey = evt.getOperationKey();

        long projectId;
        for (Iterator var4 = dataEntitys.iterator(); var4.hasNext(); SrcBidOpenFacade.setProjectOpenStatus(projectId)) {
            ExtendedDataEntity extDataEntity = (ExtendedDataEntity) var4.next();
            DynamicObject negBillObj = extDataEntity.getDataEntity();
            String negotiatetype = negBillObj.getString("negotiatetype");
            projectId = SrmCommonUtil.getPkValue(negBillObj.getDynamicObject("project"));
            boolean isNegOpen = StringUtils.equals(SrcNegotiatetypeEnums.OFFLINE_PURLIST.getValue(), negotiatetype);
            isNegOpen = isNegOpen || StringUtils.equals(SrcNegotiatetypeEnums.OFFLINE_PACKAGE.getValue(), negotiatetype);
            isNegOpen = isNegOpen || StringUtils.equals("negopen", opKey);
            if (isNegOpen) {
                if (!StringUtils.equals("negopen", opKey)) {
                    SrcNegOpenFacade.updateNegBizStatus(negBillObj);
                }

                this.negOpenHandle(negBillObj, projectId);
            } else {
                SrcNegOpenFacade.negAuditHandle(negBillObj);
            }
        }

    }

    private void negOpenHandle(DynamicObject billObj, long projectId) {
        SrcNegOpenFacade.negOpenHandle(billObj);
        SrcNegOpenFacade.negDetailsHandle(billObj);
        DynamicObject project = billObj.getDynamicObject("project");
        long fid = project.getLong("fid");
        DynamicObject projectRule = BusinessDataServiceHelper.loadSingle(fid, "src_project_rule");
        DynamicObject extfilter = projectRule.getDynamicObject("extfilter");
        if (extfilter != null) {
            String number = extfilter.getString("number");
            if ("negotiate004".equals(number)) {
                return;
            }
        }
        SrcBidOpenFacade.synthCalculate("src_compare", projectId);
    }
}